//index.js
//获取应用实例
const app = getApp();
var util = require('../../../utils/util.js');
var dataArea
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userArea: app.globalData.Maintenance.userArea,
    allProvinceCity:null,
    provinceList: null,
    cityList: null,
    districtList: null,
    chooseProvince: null,
    areaProvince: true,
    buttonClicked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    dataArea = options;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var _this = this
    wx.showLoading({
      title: '加载中',
    })
    // 获取所有省
    _this.setData({
      provinceList: app.globalData.allArea.result[0]
    })
    _this.cityList(dataArea.provinceId, dataArea.cityName);
    // 取消加载中
    wx.hideLoading();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  cityList(provinceId, cityName){
    var _this = this;
    var allCityList = app.globalData.allArea.result[1];
    var alldistrictList = app.globalData.allArea.result[2];

    var cityList = [];
    // 先添加一个
    var chooseProvince = {
      name: cityName,
      districtList: []
    }
    // 获得前3位代
    var proBef = provinceId.match(/^.{0,2}/)[0];
    // 以proBef 开头
    proBef = new RegExp("^"+proBef);
    // 去allProvinceCity找city
    for (var i=0; i < allCityList.length; i++){
      if (allCityList[i].id.search(proBef) != '-1'){
        cityList.push(allCityList[i]);
      }
    }
    console.log('cityList=', cityList);
    for (var a=0; a < cityList.length; a++){
      // 判断是否是直辖市 判断是否由两个零结尾
      if (cityList[a].id.search(/00$/g) != '-1'){
        // 是以两个零结尾的
        // 获得前4位代
        var cityBef = cityList[a].id.match(/^.{0,4}/)[0];
        // 以cityBef 开头
        cityBef = new RegExp("^"+cityBef);
        // 去alldistrictList 找区
        var districtList = []
        for (var b = 0; b < alldistrictList.length; b++) {
          if (alldistrictList[b].id.search(cityBef) != '-1') {
            districtList.push(alldistrictList[b]);
          }
        }
        cityList[a]["districtList"] = districtList;
      }else{
        chooseProvince.districtList.push(cityList[a]);
      }
    }
    if (chooseProvince.districtList.length && chooseProvince.districtList.length < cityList.length){
      chooseProvince.name = '其他'
    }
    for (var y = 0; y < cityList.length; y++){
      var a = cityList[y].districtList
      if(a){
        for (var z = 0; z < a.length; z++) {
          a[z].isChecked = false
        }
      }
    }
    for (var p = 0; p < chooseProvince.districtList.length; p++) {
      chooseProvince.districtList[p].isChecked = false
    }
    _this.setData({
      cityList: cityList,
      chooseProvince: chooseProvince,
      areaProvince: false,
      userArea: {
        province: cityName,
        city:'',
        district:'',
        adcode:''
      }
    })
    console.log('city', _this.data.cityList);
    console.log('chooseProvince', _this.data.chooseProvince)
  },
  chooseAreaIcon:function(e){
    var _this = this;
    // 循环，判断是否超过5个
    var index = 0;
    var cityList = _this.data.cityList, 
        chooseProvince = _this.data.chooseProvince,
        cityIndex = e.currentTarget.dataset.cityIndex,
        districtIndex = e.currentTarget.dataset.districtIndex;

    
    if (!cityList[cityIndex].districtList[districtIndex].isChecked){
      for (var y = 0; y < cityList.length; y++) {
        var a = cityList[y].districtList
        if (a) {
          for (var z = 0; z < a.length; z++) {
            if (a[z].isChecked) {
              index++
            }
          }
        }
      }
      for (var p = 0; p < chooseProvince.districtList.length; p++) {
        if (chooseProvince.districtList[p].isChecked) {
          index++
        }
      }
      if (index < 5) {
        cityList[cityIndex].districtList[districtIndex].isChecked = !cityList[cityIndex].districtList[districtIndex].isChecked;
        _this.setData({
          cityList: cityList
        })
      } else {
        //提示 不能超过5个
        util.alertMsg('提示', '服务区域在5个以内', function () {
          return;
        }, function () {
          console.log('用户点击取消');
          return;
        });
      }
    }else{
      cityList[cityIndex].districtList[districtIndex].isChecked = !cityList[cityIndex].districtList[districtIndex].isChecked;
      _this.setData({
        cityList: cityList
      })
    }
    
  },
  chooseAreaIconTow:function(e){
    var _this = this;
    
    // 循环，判断是否超过5个
    var index = 0;
    var cityList = _this.data.cityList, chooseProvince = _this.data.chooseProvince;
    var districtIndex = e.currentTarget.dataset.districtIndex;
    if (!chooseProvince.districtList[districtIndex].isChecked){
      for (var y = 0; y < cityList.length; y++) {
        var a = cityList[y].districtList
        if (a) {
          for (var z = 0; z < a.length; z++) {
            if (a[z].isChecked) {
              index++
            }
          }
        }
      }
      for (var p = 0; p < chooseProvince.districtList.length; p++) {
        if (chooseProvince.districtList[p].isChecked) {
          index++
        }
      }
      if (index < 5) {
        chooseProvince.districtList[districtIndex].isChecked = !chooseProvince.districtList[districtIndex].isChecked;
        _this.setData({
          chooseProvince: chooseProvince
        })
      } else {
        //提示 不能超过5个
        util.alertMsg('提示', '父亲区域在5个以内', function () {
          return;
        }, function () {
          console.log('用户点击取消');
          return;
        });
      }
    }else{
      chooseProvince.districtList[districtIndex].isChecked = !chooseProvince.districtList[districtIndex].isChecked;
      _this.setData({
        chooseProvince: chooseProvince
      })
    }
  },
  chooseArea(e){
    var _this = this;
    var cityList = _this.data.cityList, chooseProvince = _this.data.chooseProvince;
    var districtIndex = e.currentTarget.dataset.districtIndex;
    var serviceDistrict = [], serviceDistrictText=''
    for (var y = 0; y < cityList.length; y++) {
      var a = cityList[y].districtList
      if (a) {
        for (var z = 0; z < a.length; z++) {
          if (a[z].isChecked) {
            serviceDistrict.push(a[z]);
            serviceDistrictText = serviceDistrictText + a[z].fullname+'、 '
          }
        }
      }
    }
    for (var p = 0; p < chooseProvince.districtList.length; p++) {
      if (chooseProvince.districtList[p].isChecked) {
        serviceDistrict.push(chooseProvince.districtList[p]);
        serviceDistrictText = serviceDistrictText + chooseProvince.districtList[p].fullname + '、 '
      }
    }
    // 返回上一个页面
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      serviceDistrict: serviceDistrict,
      serviceDistrictText: serviceDistrictText
    })
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateBack({
        delta: 1
      })
    }
  }
})

